package cn.wildfirechat.app.config;

import cn.wildfirechat.app.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

@Service
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    public RestResult.RestCode sendVerificationCode(String toEmail, String code) throws MessagingException, UnsupportedEncodingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setTo(toEmail);
        helper.setSubject("Bird Chat 验证码 / Verification Code");
        String senderEmail = "1905333495@qq.com";
        String senderName = "Bird Chat";
        helper.setFrom(new InternetAddress(senderEmail, senderName, "UTF-8"));

        // HTML 内容（可提取为 Thymeleaf 模板或字符串替换）
        String html = getHtmlTemplate(code);

        helper.setText(html, true); // 第二个参数为 true 表示发送 HTML

        mailSender.send(message);
        return RestResult.RestCode.SUCCESS;
    }

    private String getHtmlTemplate(String code) {
        return """
                <!DOCTYPE html>
                <html lang="zh-CN">
                <head>
                  <meta charset="UTF-8">
                  <title>Bird Chat 验证码 / Verification Code</title>
                  <style>
                    body { font-family: 'Arial'; background-color: #f4f6f8; padding: 40px; }
                    .container { max-width: 600px; margin: 0 auto; background: #fff; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.05); }
                    .header h1 { text-align: center; color: #2c3e50; }
                    .code-box { text-align: center; margin: 30px 0; }
                    .code { background: #f1f3f5; padding: 15px 30px; font-size: 24px; letter-spacing: 6px; color: #2d8cf0; border-radius: 6px; font-weight: bold; display: inline-block; }
                    .footer { color: #888; font-size: 12px; text-align: center; margin-top: 40px; line-height: 1.5; }
                  </style>
                </head>
                <body>
                  <div class="container">
                    <div class="header">
                      <h1>Bird Chat 验证码 / Verification Code</h1>
                    </div>
                    <p>您好，</p>
                    <p>您正在使用 <strong>Bird Chat</strong> 进行身份验证。请输入以下验证码完成操作：</p>
                    <p>Hello,</p>
                    <p>You are verifying your identity for <strong>Bird Chat</strong>. Please enter the following code to continue:</p>
                    <div class="code-box">
                      <div class="code">""" + code  + """
                      </div>
                    </div>
                    <p>⚠️ 验证码有效期 5 分钟。/ Code is valid for 5 minutes.</p>
                    <div class="footer">
                      此邮件由 Bird Chat 系统自动发送，请勿直接回复。<br>
                      This is an automated message from Bird Chat. Please do not reply.
                    </div>
                  </div>
                </body>
                </html>
                """;
    }
}